﻿using BacklogParser.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace BacklogParser.Parsers
{
    public static class LicenseDataParser
    {
        public static List<LicenseData> Parse(List<XElement> licenses)
        {
            var result = new List<LicenseData>();
            
            foreach(var item in licenses)
            {
                var licenseesList = new List<Licensee>();
                var licensees = item.Descendants(Constants.Epo + "licensee").ToList();

                foreach(var licensee in licensees)
                {
                    var designation = licensee.Attribute("designation").Value;
                    var sequence = licensee.Attribute("sequence").Value;
                    var typeLicense = licensee.Attribute("type-license").Value;

                    var addressbook = licensee.Descendants(Constants.Epo + "addressbook").FirstOrDefault();
                    var effectiveIn = licensee.Descendants(Constants.Epo + "effective-in").FirstOrDefault();
                    var date = licensee.Descendants(Constants.Epo + "date").FirstOrDefault();

                    var licenseeInstance = new Licensee()
                    {
                        Designation = designation,
                        Sequence = sequence,
                        TypeLicense = typeLicense
                    };

                    if(addressbook != null)
                    {
                        var addres = addressbook.Descendants(Constants.Epo + "address").Descendants().ToList().Select(x => x.Value).ToList();
                        List<Address> addresses = addres.Select(x => new Address { Location = x }).ToList();
                        licenseeInstance.Addresses = addresses;

                        licenseeInstance.Name = addressbook.Descendants(Constants.Epo + "name").First().Value;
                        licenseeInstance.Country = addressbook.Descendants(Constants.Epo + "country").First().Value;
                        licenseeInstance.Name = addressbook.Descendants(Constants.Epo + "name").First().Value;
                    }

                    if(effectiveIn != null)
                    {
                        var countries = effectiveIn.Descendants(Constants.Epo + "country").ToList().Select(x => x.Value);
                        licenseeInstance.EffectiveIn = string.Join(" ", countries);
                    }

                    licenseeInstance.Date = date != null ? date.Value : "";
                    licenseesList.Add(licenseeInstance);

                }

                var license = new LicenseData
                {
                    Licensees = licenseesList,
                };

                result.Add(license);
            }

            return result;
        }

        public class Licensee
        {
            public Licensee()
            {
                this.Addresses = new List<Address>();
            }
            public string  Name { get; set; }

            public List<Address> Addresses { get; set; }

            public string Country { get; set; }

            public string Date { get; set; }

            public string EffectiveIn { get; set; }

            public string Designation { get; set; }

            public string Sequence { get; set; }

            public string TypeLicense { get; set; }

            public string Address { get { return string.Join(", ", this.Addresses); } }

        }
    }
}
